<template>
  <el-dialog
    v-model="props.fileTypeDialog"
    title="新增类型"
    width="25%"
    :show-close="false"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
      size="default"
      status-icon
    >
      <el-form-item label="字典名称" prop="configValue">
        <el-input
          v-model="ruleForm.configValue"
          placeholder="请输入字典名称"
          show-word-limit
          maxlength="12"
        />
      </el-form-item>
      <div style="text-align: right">
        <el-button @click="quxiao(ruleFormRef)"> 取消 </el-button>
        <el-button
          type="primary"
          @click="queren(ruleFormRef)"
          :loading="loading"
          >确认</el-button
        >
      </div>
    </el-form>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { FormRules, FormInstance, ElMessage } from 'element-plus'
import { addDictionary } from '/@/api/publicApi'
const ruleFormRef = ref<FormInstance>()
const props = defineProps({
  fileTypeDialog: Boolean,
  typeString: String
})
const emit = defineEmits(['submitFileType'])
const loading = ref(false)

const ruleForm = ref({
  parentId: 0,
  configType: props.typeString,
  configName: '',
  configValue: ''
})

const rules = reactive<FormRules>({
  configValue: [{ required: true, message: '请输入字典名称', trigger: 'blur' }]
})

const quxiao = (formEl: FormInstance | undefined) => {
  emit('submitFileType', ruleForm.value.configValue)
  if (!formEl) return
  formEl.resetFields()
}

const queren = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      addDictionary(ruleForm.value).then((res) => {
        if (res.code == 0) {
          loading.value = false
          ElMessage.success('添加成功')
          quxiao(formEl)
        } else {
          loading.value = false
        }
      })
    } else {
      loading.value = false
    }
  })
}
</script>
<style scoped lang="scss"></style>
